<?php

class Role extends Api
{
    public function add()
    {
        $name = $this->getParam('name');
        $roles = $this->getParam('roles');
        if ($name == '') {
            $this->backJson([], ApiConst::BACK_PARAM_ERROR);
        }
        if (!$this->isAdmin) {
            $this->getDb()->where("pid", $this->userInfo['role']);
            $role = $this->getDb()->getOne('roles');
            $parentRoles = explode(',', $role['roles']);
        } else {
            $parentRoles = array_column(AuthConst::AUTH, 'id');
        }
        if (!$this->isAdmin) {
            $this->getDb()->where("pid", $this->userInfo['role']);
            $info = $this->getDb()->getOne('roles', 'count(0) as nums');
            if ($info['nums'] >= 5) {
                $this->backJson([], ApiConst::BACK_ROLE_LIMIT);
            }
        }
        $addRoles = [];
        foreach ($roles as $role) {
            if (in_array($role, $parentRoles)) {
                $addRoles[] = $role;
            }
        }
        if ($this->getDb()->insert('roles', [
            'name'       => $name,
            'roles'      => implode(',', $addRoles),
            'pid'        => $this->userInfo['role'],
            'created_at' => time()
        ])) {
            $this->backJson([]);
        }
        $this->backJson([], ApiConst::BACK_CODE_ERROR);
    }


    public function edit()
    {
        $id   = $this->getParam('id');
        $name = $this->getParam('name');
        $roles = $this->getParam('roles');
        if (!$this->isAdmin) {
            $this->getDb()->where("id", $this->userInfo['role']);
            $role = $this->getDb()->getOne('roles');
            $parentRoles = explode(',', $role['roles']);
        } else {
            $this->getDb()->where("id", $id);
            $info = $this->getDb()->getOne('roles');
            $pid = $info['pid'];
            if ($pid > 0) {
                $this->getDb()->where("id", $pid);
                $role = $this->getDb()->getOne('roles');
                $parentRoles = explode(',', $role['roles']);
            } else {
                $parentRoles = array_column(AuthConst::AUTH, 'id');
            }
        }
        if (empty($parentRoles)) {
            $this->backJson([], ApiConst::BACK_NOT_EXIST_INFO);
        }
        $data = [];
        if ($name) {
            $data['name'] = $name;
        }
        $upRoles = [];
        foreach ($roles as $role) {
            if (in_array($role, $parentRoles)) {
                $upRoles[] = $role;
            }
        }
        $data['roles'] = implode(',', $upRoles);
        $this->getDb()->where('id', $id);
        if (!$this->isAdmin) {
            $this->getDb()->where("pid", $this->userInfo['role']);
        }
        if ($this->getDb()->update('roles', $data)) {
            $this->backJson([]);
        }
        $this->backJson([], ApiConst::BACK_CODE_ERROR);
    }

    public function list()
    {
        $page     = (int)$this->getParam('page', 1);
        $pageSize = 20;
        if (!$this->isAdmin) {
            $this->getDb()->where("pid", $this->userInfo['role']);
        }
        $this->getDb()->pageLimit = $pageSize;
        $this->getDb()->orderBy('id', 'ASC');
        $list = $this->getDb()->paginate("roles", $page, 'id,name,status,created_at');
        foreach ($list as $k => $item) {
            $list[$k]['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
        }
        $this->backJson([
            'list'      => $list,
            'total'     => (int)$this->getDb()->totalCount,
            'page'      => (int)$page,
            'totalPage' => (int)ceil($this->getDb()->totalCount / $pageSize),
            'pageSize'  => (int)$pageSize
        ]);
    }


    public function status()
    {
        $id     = (int)$this->getParam('id', 0);
        $status = (int)$this->getParam('status');
        if (!in_array($status, [0, 1])) {
            $this->backJson([], ApiConst::BACK_PARAM_ERROR);
        }
        $this->getDb()->where("id", $id);
        $info = $this->getDb()->getOne('roles', 'status');
        if ($info['status'] == $status) {
            $this->backJson([]);
        }
        $this->getDb()->where("id", $id);
        $this->getDb()->update('roles', ['status' => $status]);
        $this->backJson([]);
    }

    public function menu()
    {
        $id = (int)$this->getParam('id', 0);
        if ($id > 0) {
            $this->getDb()->where('id', $id);
            $roleInfo = $this->getDb()->getOne('roles', 'roles,pid');
            $roles = explode(',', $roleInfo['roles']);
        } else {
            $roles = [];
        }
        $data = [];
        foreach (AuthConst::AUTH as $item) {
            $data[] = [
                'id' => $item['id'],
                'name' => $item['name'],
                'pid' => $item['pid'],
                'open' => true,
                'checked' => in_array($item['id'], $roles)
            ];
        }
        $this->backJson(['menu' => $data]);
    }

    public function auth()
    {
        $id = (int)$this->getParam('id', 0);
        if ($id > 0) {
            $this->getDb()->where('id', $id);
            $auths = $this->getDb()->getOne('roles', 'auth,pid');
            $auth  = $auths['auth'];
            $role  = $auths['pid'];
        } else {
            $role = $this->userInfo['role'];
            $auth = '';
        }
        $auth = explode(',', $auth);
        if ($role == 0) {
            $allAuth = [];
            foreach (AuthConst::AUTH as $key => $val) {
                $allAuth[] = $key;
            }
        } else {
            $this->getDb()->where('id', $role);
            $allAuth = $this->getDb()->getOne('roles', 'auth');
            $allAuth = explode(',', $allAuth['auth']);
        }
        $allAuth     = array_map(function ($val) {
            return (string)$val;
        }, $allAuth);
        $allAuthName = [];
        foreach ($allAuth as $value) {
            $allAuthName[$value] = AuthConst::AUTH[$value];
        }
        $this->backJson(['auth' => $auth, 'allAuth' => $allAuth, 'allAuthName' => $allAuthName]);
    }

}
